xquery version "1.0-ml";

module namespace text = "http://www.porvino.com/text-input";

(:
accepts arguments as xml 
<component>
    <label>varietal</label>
    <function>text:get-text-input-controls</function>
    <function-id>blendvarietal</function-id>
    <host>{$host}</host>
    <doc-id>{$g_id}</doc-id>
    <data-node>/wine/blend/blendPart/blendVarietal</data-node>
    <style>width:120px;</style>
</component>

let $cmd := fn:concat($ns," ",$c/input-group:function/text(),"('",$c/input-group:function-id/text(),"','",$c/input-group:host/text(),"','",
                                          $c/input-group:doc-id/text(),"','", $data-root ,"[",$count,"]/",$c/input-group:data-node/text(),"','",$c/input-group:style/text(),"')")

NOT TESTED
:)
declare function get-text-input-controls( $args as node()) {
     get-text-input-controls (
                                $args/text:component-id/text(),
                                $args/text:host/text(),
                                $args/text:doc-id/text(),
                                $args/text:data-node/text(),
                                $args/text:style/text()
                             )
};

declare function get-text-input-controls( $cid as xs:string, 
                                          $host as xs:string,
                                          $did as xs:string,
                                          $dbase as xs:string,
                                          $style as xs:string) {
     
     <input xmlns="http://www.w3.org/1999/xhtml"
        name="{$cid}"
        id="{$cid}"
        type="text" 
        onchange="textInput.textInputChangeHandler('{$cid}','{$host}','{$did}','{$dbase}', this.value);"
        style="{$style}" value="{xdmp:eval(fn:concat("fn:doc('",$did,"')",$dbase))}">
     </input>
};

     
 (: 
    display each component that was passed in
    by default show them in the order passed in, in a single line 
    
    for each widget supported need to add the ns below
    
    example argument for components
            <components xmlns="http://www.porvino.com/text-input">
                    <component>
                        <label>varietal</label>
                        <function>text:get-text-input-controls</function>
                        <component-id>blendvarietal</component-id>
                        <host>{$host}</host>
                        <doc-id>{$g_id}</doc-id>
                        <data-node-leaf>blendVarietal</data-node-leaf>
                        <style>width:120px;</style>
                    </component>
            </components> 
 
 the leaf node is provided so that get-input-group can easily create references to individual nodes that are repeated
 problems solved:
 - dynamically call the input methods as many times as needed
 - allow arbitrary combinations of a function and xml arguments for that function to be passed in
 
 :)
declare function get-input-group( $input-group-id as xs:string,      
                                  $doc-id as xs:string,
                                  $data-root as xs:string,
                                  $components as node()              (:components as a sequence :)
                                ) {
     
     <div id="{$input-group-id}" xmlns="http://www.w3.org/1999/xhtml">
        {
            let $count := 0
            for $item in xdmp:eval(fn:concat("fn:doc('",$doc-id,"')",$data-root))
                let $temp := xdmp:set($count, fn:sum(($count, 1)))
                return
                (
                for $c in $components/text:component
                    let $ns :=
                        fn:concat('import module namespace text = "http://www.porvino.com/text-input" at "/porvino/text-input/text-input.xqy";')
                    let $a := 
                        fn:concat(
                        "<component xmlns='http://www.porvino.com/text-input'>",
                            xdmp:quote($c/*),
                            "<data-node>",$data-root ,"[",$count,"]/",$c/text:data-node-leaf/text(),"</data-node>",
                        "</component>"
                        )
                    let $cmd := fn:concat($ns," ",$c/text:function/text(), "(",$a,")" )
                    return xdmp:eval($cmd)
                ,
                <br/>
                )
        }
     </div>      
};


declare function get-delete-group-row( $args as node()) { 
    get-delete-group-row(
        $args/text:component-id/text(),
        $args/text:host/text(),
        $args/text:doc-id/text(),
        $args/text:data-node/text(),
        $args/text:style/text(),
        $args/text:image/text()
    )                     
};

declare function get-delete-group-row(
      $cid as xs:string, 
      $host as xs:string,
      $did as xs:string,
      $dbase as xs:string,
      $style as xs:string,
      $image as xs:string
) { 
    <img id="{$cid}" src="{$image}" style="{$style}">
    </img>
                                
};
